package org.yty.travelagencymongodb.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.yty.travelagencymongodb.model.Entity.Guidebook;
import org.yty.travelagencymongodb.model.Entity.Result;
import org.yty.travelagencymongodb.service.GuidebookService;

@RestController
@RequestMapping("/guidebook")
@RequiredArgsConstructor
public class GuidebookController {
    private final GuidebookService guidebookService;

    @GetMapping("/{id}")
    public Result<Guidebook> getGuidebookById(@PathVariable Long id){
        Guidebook result = this.guidebookService.getById(id);
        if (result != null){
            return Result.ok(result);
        } else {
            return Result.error("导游不存在");
        }
    }

    @PostMapping
    public Result<Void> saveGuidebook(@RequestBody Guidebook guidebook){
        boolean result = this.guidebookService.save(guidebook);
        if (result){
            return Result.ok();
        } else {
            return Result.error("导游不存在");
        }
    }

    @PutMapping
    public Result<Void> updateGuidebook(@RequestBody Guidebook guidebook){
        boolean result = this.guidebookService.update(guidebook);
        if (result){
            return Result.ok();
        } else {
            return Result.error("导游不存在");
        }
    }


    @DeleteMapping("/{id}")
    public Result<Void> deleteGuidebookById(@PathVariable Long id){
        this.guidebookService.removeById(id);
        return Result.ok();
    }
}
